﻿using Mapster;
using MESPro.DBManager;
using MESPro.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MESPro.Views
{
    public partial class UserAddRole : Form
    {
        public UserAddRole()
        {
            InitializeComponent();
            this.Load += AddRole_Load;
        }
        List<User> users = new();
        private List<Role> src;
        private List<Role> dic;

        private void AddRole_Load(object sender, EventArgs e)
        {
            users = DbContext.Db.Queryable<User>().Includes(x => x.Roles).ToList();
            this.list_people.DataSource = users;
            this.list_people.DisplayMember = nameof(User.UserName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="src"></param>
        /// <param name="dic"></param>
        //public AddRole(List<T> src, List<T> dic)
        //{
        //    InitializeComponent();
        //    //var srcc = src.Adapt<List<Role>>();
        //    //var dicc = dic.Adapt<List<Role>>();
        //    //this.list_src.DataSource = srcc;
        //    //this.list_dic.DataSource = dicc;
        //}
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void srcTodic(object sender, EventArgs e)
        {
            var s = this.list_src.SelectedItems.Cast<Role>().ToList();//要添加的
            var d = this.list_dic.SelectedItems.Cast<Role>().ToList();//要删除的
            foreach (var x in s)
            {
                src.Remove(x);
                dic.Add(x);
            }
            this.list_src.DataSource = null;
            this.list_dic.DataSource = null;
            this.list_src.DataSource = src;
            this.list_dic.DataSource = dic;
            list_src.DisplayMember = nameof(Role.RoleName);
            list_src.ValueMember = "Id";

            list_dic.DisplayMember = nameof(Role.RoleName);
            list_dic.ValueMember = "Id";
        }
        /// <summary>
        /// 减少权限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dicTosrc(object sender, EventArgs e)
        {
            var s = this.list_src.SelectedItems.Cast<Role>().ToList();//要删除的
            var d = this.list_dic.SelectedItems.Cast<Role>().ToList();//要增加的
            foreach (var x in d)
            {
                src.Add(x);
                dic.Remove(x);
            }
            this.list_src.DataSource = null;
            this.list_dic.DataSource = null;
            this.list_src.DataSource = src;
            this.list_dic.DataSource = dic;
            list_src.DisplayMember = nameof(Role.RoleName);
            list_src.ValueMember = "Id";

            list_dic.DisplayMember = nameof(Role.RoleName);
            list_dic.ValueMember = "Id";
        }

        private void list_people_SelectedIndexChanged(object sender, EventArgs e)
        {
            var s = this.list_people.SelectedItem as User;
            this.list_dic.DataSource = null;
            dic = s.Roles;
            this.list_dic.DataSource = dic;
            this.list_src.DataSource = null;
            src = DbContext.Db
                .Queryable<Role>()
                .Where(x=>!s.Roles.Any(y=>y.Id==x.Id)).ToList();
            this.list_src.DataSource = src;
            this.list_src.DisplayMember=nameof(Role.RoleName);
            this.list_dic.DisplayMember=nameof(Role.RoleName);
            list_src.ValueMember = "Id";
            list_dic.ValueMember = "Id";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var user = this.list_people.SelectedItem as User;
            user.Roles = this.list_dic.DataSource as List<Role>;

            List<User> List = new List<User>() { user };
            var res = DbContext.Db.UpdateNav(List)
                         .Include(z1 => z1.Roles)
                         .ExecuteCommand();
            MainServerFrm.RMShowMsg?.Invoke(res ? "ok" : "ng", Enums.MsgLevel.Trace);
            this.Close();
        }
    }
}
